package com.cloudcc.mobile.view.main.fragment;

import android.view.View;
import android.widget.ListView;
import butterknife.Bind;

import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreListViewContainer;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrUIHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.MaterialHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.indicator.PtrIndicator;
import com.cloudcc.cloudframe.util.DensityUtils;
import com.cloudcc.mobile.R;

public abstract class MaterialListFragment extends BaseListFragment implements PtrUIHandler {

	@Bind(R.id.listview)
	protected ListView mListView;

	@Bind(R.id.load_more_list_view_ptr_frame)
	PtrFrameLayout mRefreshLayout;

	@Bind(R.id.load_more_list_view_container)
	LoadMoreListViewContainer mLoadMoreLayout;
	
	
	@Override
	public void requestData() {

	}

	protected void initRefresh() {
		mRefreshLayout.setLoadingMinTime(1000);
		mRefreshLayout.setPtrHandler(new PtrHandler() {
			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame,
						mListView, header);
			}

			@Override
			public void onRefreshBegin(PtrFrameLayout frame) {
				onRefreshData();
			}
		});
		mRefreshLayout.addPtrUIHandler(this);

		// header place holder
		final MaterialHeader header = new MaterialHeader(getActivity());
		int[] colors = getResources().getIntArray(R.array.google_colors);
		header.setColorSchemeColors(colors);
		header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
		header.setPadding(0, DensityUtils.dpToPixel(mContext, 15), 0,
				DensityUtils.dpToPixel(mContext, 10));

		header.setPtrFrameLayout(mRefreshLayout);
		mRefreshLayout.setHeaderView(header);
		mRefreshLayout.addPtrUIHandler(header);

		// auto load data
		mRefreshLayout.postDelayed(new Runnable() {
			@Override
			public void run() {
				mRefreshLayout.autoRefresh(true);
			}
		}, 150);
	}

	@Override
	public void onUIPositionChange(PtrFrameLayout arg0, boolean arg1,
			byte arg2, PtrIndicator arg3) {
		
	}

	@Override
	public void onUIRefreshBegin(PtrFrameLayout arg0) {
		
	}

	@Override
	public void onUIRefreshComplete(PtrFrameLayout arg0) {
		
	}

	@Override
	public void onUIRefreshPrepare(PtrFrameLayout arg0) {
		
	}

	@Override
	public void onUIReset(PtrFrameLayout arg0) {
		
	}


}
